package io.adrainty.bolee.insurance.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.adrainty.bolee.framework.mybatis.utils.MybatisUtil;
import io.adrainty.bolee.insurance.mapper.*;
import io.adrainty.bolee.insurance.models.bo.*;
import io.adrainty.bolee.insurance.models.entity.WarrantyEarningsOrderEntity;
import io.adrainty.bolee.insurance.models.entity.WarrantyEntity;
import io.adrainty.bolee.insurance.models.entity.WarrantyInsuredEntity;
import io.adrainty.bolee.insurance.models.entity.WarrantyOrderEntity;
import io.adrainty.bolee.insurance.repository.IWarrantyRepository;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/26 上午12:54
 */

@Repository
public class WarrantyRepositoryImpl implements IWarrantyRepository {

    @Resource
    private WarrantyMapper warrantyMapper;

    @Resource
    private WarrantyOrderMapper warrantyOrderMapper;

    @Resource
    private WarrantyInsuredMapper warrantyInsuredMapper;

    @Resource
    private WarrantyEarningsOrderMapper warrantyEarningsOrderMapper;

    @Resource
    private WarrantyVerifyMapper warrantyVerifyMapper;

    @Override
    public void saveEarningsOrderBatch(List<WarrantyEarningsOrderEntity> warrantyEarningsOrderList) {
        List<WarrantyEarningsOrderBO> warrantyEarningsOrderBOList = BeanUtil.copyToList(warrantyEarningsOrderList, WarrantyEarningsOrderBO.class);
        warrantyEarningsOrderMapper.insert(warrantyEarningsOrderBOList);
    }

    @Override
    public void saveWarranty(WarrantyEntity warrantyEntity) {
        WarrantyBO warrantyBO = BeanUtil.copyProperties(warrantyEntity, WarrantyBO.class);
        warrantyMapper.insert(warrantyBO);
        warrantyEntity.setId(warrantyBO.getId());
    }

    @Override
    public void updateWarranty(WarrantyEntity warrantyEntity) {
        WarrantyBO warrantyBO = BeanUtil.copyProperties(warrantyEntity, WarrantyBO.class);
        warrantyMapper.updateById(warrantyBO);
    }

    @Override
    public void saveOrderBatch(List<WarrantyOrderEntity> orderEntities) {
        List<WarrantyOrderBO> warrantyOrderBOList = BeanUtil.copyToList(orderEntities, WarrantyOrderBO.class);
        warrantyOrderMapper.insert(warrantyOrderBOList);
    }

    @Override
    public void saveInsuredBatch(List<WarrantyInsuredEntity> insuredEntities) {
        List<WarrantyInsuredBO> warrantyInsuredBOList = BeanUtil.copyToList(insuredEntities, WarrantyInsuredBO.class);
        warrantyInsuredMapper.insert(warrantyInsuredBOList);
    }

    @Override
    public List<WarrantyEntity> queryWarranty(WarrantyEntity queryDTO) {
        Wrapper<WarrantyBO> wrapper = getWarrantyWrapper(queryDTO);
        List<WarrantyBO> warrantyBOList = warrantyMapper.selectList(wrapper);
        return BeanUtil.copyToList(warrantyBOList, WarrantyEntity.class);
    }

    @Override
    public void deleteWarranty(Long id) {
        warrantyMapper.deleteById(id);
    }

    @Override
    public void deleteEarningsOrderByWarrantyNo(String warrantyNo) {
        Wrapper<WarrantyEarningsOrderBO> wrapper = Wrappers.lambdaQuery(WarrantyEarningsOrderBO.class)
                .eq(WarrantyEarningsOrderBO::getWarrantyNo, warrantyNo);
        warrantyEarningsOrderMapper.delete(wrapper);
    }

    @Override
    public void deleteInsuredByWarrantyNo(String warrantyNo) {
        Wrapper<WarrantyInsuredBO> wrapper = Wrappers.lambdaQuery(WarrantyInsuredBO.class)
                .eq(WarrantyInsuredBO::getWarrantyNo, warrantyNo);
        warrantyInsuredMapper.delete(wrapper);
    }

    @Override
    public void deleteOrderByWarrantyNo(String warrantyNo) {
        Wrapper<WarrantyOrderBO> wrapper = Wrappers.lambdaQuery(WarrantyOrderBO.class)
                .eq(WarrantyOrderBO::getWarrantyNo, warrantyNo);
        warrantyOrderMapper.delete(wrapper);
    }

    @Override
    public void deleteVerifyByWarrantyNo(String warrantyNo) {
        Wrapper<WarrantyVerifyBO> wrapper = Wrappers.lambdaQuery(WarrantyVerifyBO.class)
                .eq(WarrantyVerifyBO::getWarrantyNo, warrantyNo);
        warrantyVerifyMapper.delete(wrapper);
    }

    @Override
    public WarrantyOrderEntity findValidOrderById(Long warrantyOrderId) {
        WarrantyOrderBO orderBO = warrantyOrderMapper.selectById(warrantyOrderId);
        return BeanUtil.toBean(orderBO, WarrantyOrderEntity.class);
    }

    private Wrapper<WarrantyBO> getWarrantyWrapper(WarrantyEntity queryDTO) {
        return Wrappers.lambdaQuery(WarrantyBO.class)
                .eq(queryDTO.getInsuranceId() != null, WarrantyBO::getInsuranceId, queryDTO.getInsuranceId())
                .eq(StringUtils.isNotBlank(queryDTO.getInsuranceName()), WarrantyBO::getInsuranceName, queryDTO.getInsuranceName())
                .eq(StringUtils.isNotBlank(queryDTO.getInsuranceJson()), WarrantyBO::getInsuranceJson, queryDTO.getInsuranceJson())
                .eq(StringUtils.isNotBlank(queryDTO.getWarrantyNo()), WarrantyBO::getWarrantyNo, queryDTO.getWarrantyNo())
                .in(CollUtil.isNotEmpty(queryDTO.getWarrantyNos()), WarrantyBO::getWarrantyNo, queryDTO.getWarrantyNos())
                .eq(StringUtils.isNotBlank(queryDTO.getApplicantName()), WarrantyBO::getApplicantName, queryDTO.getApplicantName())
                .eq(StringUtils.isNotBlank(queryDTO.getApplicantIdentityCard()), WarrantyBO::getApplicantIdentityCard, queryDTO.getApplicantIdentityCard())
                .eq(StringUtils.isNotBlank(queryDTO.getCompanyNo()), WarrantyBO::getCompanyNo, queryDTO.getCompanyNo())
                .eq(StringUtils.isNotBlank(queryDTO.getCompanyName()), WarrantyBO::getCompanyName, queryDTO.getCompanyName())
                .eq(queryDTO.getSafeguardStartTime() != null, WarrantyBO::getSafeguardStartTime, queryDTO.getSafeguardStartTime())
                .eq(queryDTO.getSafeguardEndTime() != null, WarrantyBO::getSafeguardEndTime, queryDTO.getSafeguardEndTime())
                .eq(queryDTO.getPremium() != null, WarrantyBO::getPremium, queryDTO.getPremium())
                .eq(StringUtils.isNotBlank(queryDTO.getAutoWarrantyExtension()), WarrantyBO::getAutoWarrantyExtension, queryDTO.getAutoWarrantyExtension())
                .eq(queryDTO.getWarrantyState() != null, WarrantyBO::getWarrantyState, queryDTO.getWarrantyState())
                .in(ArrayUtils.isNotEmpty(queryDTO.getWarrantyStates()), WarrantyBO::getWarrantyState, MybatisUtil.convertCheckIds(queryDTO.getWarrantyStates()))
                .eq(queryDTO.getPeriods() != null, WarrantyBO::getPeriods, queryDTO.getPeriods())
                .eq(queryDTO.getDutyPeriod() != null, WarrantyBO::getDutyPeriod, queryDTO.getDutyPeriod())
                .eq(queryDTO.getHesitationTime() != null, WarrantyBO::getHesitationTime, queryDTO.getHesitationTime())
                .eq(queryDTO.getGrace() != null, WarrantyBO::getGrace, queryDTO.getGrace())
                .eq(StringUtils.isNotBlank(queryDTO.getGraceUnit()), WarrantyBO::getGraceUnit, queryDTO.getGraceUnit())
                .eq(queryDTO.getRevival() != null, WarrantyBO::getRevival, queryDTO.getRevival())
                .eq(StringUtils.isNotBlank(queryDTO.getRevivalUnit()), WarrantyBO::getRevivalUnit, queryDTO.getRevivalUnit())
                .eq(StringUtils.isNotBlank(queryDTO.getAgentId()), WarrantyBO::getAgentId, queryDTO.getAgentId())
                .eq(StringUtils.isNotBlank(queryDTO.getAgentName()), WarrantyBO::getAgentName, queryDTO.getAgentName())
                .eq(queryDTO.getDataState() != null, WarrantyBO::getDataState, queryDTO.getDataState())
                .eq(queryDTO.getUnderwritingState() != null, WarrantyBO::getUnderwritingState, queryDTO.getUnderwritingState())
                .orderByAsc(WarrantyBO::getSortNo).orderByDesc(WarrantyBO::getCreatedTime);
    }
}
